package com.xingdata.jjxc.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.CarhudEntity;
import com.xingdata.jjxc.m.interf.CarInfoItemClick;

public class CarInfomationAdp extends BaseAdapter {
	private Activity avt;
	private List<CarhudEntity> carEnList;

	private CarInfoItemClick carDelete;
	private GridView mGridView;

	public CarInfomationAdp(Activity avt, List<CarhudEntity> carEn,
			CarInfoItemClick carDelete, GridView mGridView) {
		super();
		this.avt = avt;
		this.carEnList = carEn;
		this.carDelete = carDelete;
		this.mGridView = mGridView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return carEnList == null ? 0 : carEnList.size();

	}

	@Override
	public CarhudEntity getItem(int position) {
		// TODO Auto-generated method stub
		return carEnList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(avt).inflate(
					R.layout.item_car_infomation, null);

			holder.item_car_information_lin = (LinearLayout) convertView
					.findViewById(R.id.item_car_information_lin);
			holder.item_car_info_delete_iv = (ImageView) convertView
					.findViewById(R.id.item_car_info_delete_iv);
			holder.car_infomation_iv = (ImageView) convertView
					.findViewById(R.id.car_infomation_iv);
			holder.item_car_infomation_jc_tv = (TextView) convertView
					.findViewById(R.id.item_car_infomation_jc_tv);
			holder.item_car_infomation_num_tv = (TextView) convertView
					.findViewById(R.id.item_car_infomation_num_tv);
			holder.item_car_vehicles_tv = (TextView) convertView
					.findViewById(R.id.item_car_vehicles_tv);
			holder.item_car_infomation_vehicles_tv = (TextView) convertView
					.findViewById(R.id.item_car_infomation_vehicles_tv);
			holder.item_car_engine_number_tv = (TextView) convertView
					.findViewById(R.id.item_car_engine_number_tv);
			holder.item_car_frame_number_tv = (TextView) convertView
					.findViewById(R.id.item_car_frame_number_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final CarhudEntity com = getItem(position);
		holder.car_infomation_iv.setImageResource(R.drawable.add_bus_car);
		holder.item_car_infomation_jc_tv.setText(com.getCar_id());
		holder.item_car_infomation_num_tv.setText(com.getCar_number());
		holder.item_car_vehicles_tv.setText(com.getEngine_number());
		holder.item_car_infomation_vehicles_tv.setText(com.getEngine_number());

		holder.item_car_engine_number_tv.setText(com.getHudsn());
		holder.item_car_frame_number_tv.setText(com.getUnderpan_number());

		final ImageView item_car_info_delete_iv = holder.item_car_info_delete_iv;
		holder.item_car_information_lin
				.setOnLongClickListener(new View.OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						item_car_info_delete_iv.setVisibility(View.VISIBLE);
						item_car_info_delete_iv
								.setBackgroundResource(R.drawable.delete_gray);
						carDelete.deleteClick(com, item_car_info_delete_iv,
								position);
						if (mGridView != null) {
							for (int i = 0; i < mGridView.getChildCount(); i++) {
								if (i != position) {
									// 获取GridView里面的item
									RelativeLayout mView = (RelativeLayout) mGridView
											.getChildAt(i);
									for (int j = 0; j < mView.getChildCount(); j++) {
										// 获取ITEM里面的子View
										View mLayout = mView.getChildAt(j);
										if (mLayout instanceof FrameLayout) {
											ImageView imageView = (ImageView) mLayout
													.findViewById(R.id.item_car_info_delete_iv);
											imageView
													.setVisibility(View.INVISIBLE);
										}

									}
								}
							}

						}

						return true;
					}
				});
		holder.item_car_information_lin
				.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						carDelete.onIntentClick(com, position);
					}
				});

		return convertView;
	}

	final class ViewHolder {
		public ImageView car_infomation_iv;
		public TextView item_car_infomation_jc_tv, item_car_infomation_num_tv,
				item_car_vehicles_tv, item_car_infomation_vehicles_tv,
				item_car_engine_number_tv, item_car_frame_number_tv;
		/**
		 * 整块车信息
		 */
		public LinearLayout item_car_information_lin;

		public ImageView item_car_info_delete_iv;
	}

}
